package edu.cicese.alz;

import org.openstreetmap.gui.jmapviewer.MapLocation;

import java.util.ArrayList;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 26/01/12
 * Time: 06:07 PM
 */
public class LocationCluster implements Comparable<LocationCluster> {
	private int quantity = 0;
	private int time;
	private ArrayList<MapLocation> locations = new ArrayList<>();

	public void add(MapLocation l) {
		locations.add(l);
		quantity++;
	}

	public ArrayList<MapLocation> getLocations() {
		return locations;
	}

	public int compareTo(LocationCluster lc) {
		return lc.quantity - this.quantity;
	}

	public int getTimeSpent() {
		return time;
	}

	public int getQuantity() {
		return quantity;
	}

	public int calculateTimeSpent() {
		time = 0;
		for (MapLocation mapLocation : locations) {
			time += mapLocation.getTimeSpent();
		}
		return time;
	}

	public double[] getMaxMin() {
		double minLat = 90, maxLat = -90;
		double minLon = 180, maxLon = -180;
		double lat, lon;
		double minLatT, maxLatT, minLonT, maxLonT;
		double minLatAux = minLat, maxLatAux = maxLat, minLonAux = minLon, maxLonAux = maxLon;
		for (MapLocation mapLocation : locations) {
			lat = mapLocation.getLat();
			lon = mapLocation.getLon();
			minLatT = mapLocation.getMinLat();
			maxLatT = mapLocation.getMaxLat();
			minLonT = mapLocation.getMinLon();
			maxLonT = mapLocation.getMaxLon();
			if (lat < minLat) {
				minLat = lat;
			}
			if (lat > maxLat) {
				maxLat = lat;
			}
			if (lon < minLon) {
				minLon = lon;
			}
			if (lon > maxLon) {
				maxLon = lon;
			}

			if (minLatT < minLatAux) {
				minLatAux = minLatT;
			}
			if (maxLatT > maxLatAux) {
				maxLatAux = maxLatT;
			}
			if (minLonT < minLonAux) {
				minLonAux = minLonT;
			}
			if (maxLonT > maxLonAux) {
				maxLonAux = maxLonT;
			}
		}
		lat = minLat + (maxLat - minLat) / 2;
		lon = minLon + (maxLon - minLon) / 2;

		return new double[]{lat, lon, minLatAux, minLonAux, maxLatAux, maxLonAux};
	}
}
